﻿using System;

namespace Calendar
{
    class Program
    {
        public static void Main(string[] args)
        {
            DateTime today = DateTime.Now;
            //获取今日时间
            int year = today.Year;
            int month = today.Month;
            int day = DateTime.Now.Day;
            //获取年月日
            Console.Write(year);
            Console.Write("年");
            Console.Write(month);
            Console.WriteLine("月日历");


            Console.WriteLine(" 一 二 三 四 五 六 日");
            int First = Convert.ToInt32(new DateTime(year, month, 1).DayOfWeek);
            //第一天位置
            int Total = DateTime.DaysInMonth(year, month);
            //本月总天数
            int LastMonth = month - 1;
            int TotalLastMonth = DateTime.DaysInMonth(year, LastMonth);
            //上个月的月数和天数

            int j = 1;
            for (int i = 1; i <= 42; i++)
            {
                if (i >= First && j <= Total)
                {
                    if (j == day)
                    {
                        Console.ForegroundColor = ConsoleColor.White;
                        Console.BackgroundColor = ConsoleColor.Blue;
                        Console.Write(Convert.ToString(j).PadLeft(3));
                    }
                    else
                        Console.Write(Convert.ToString(j).PadLeft(3));
                    j++;
                    Console.ResetColor();
                }
                //本月

                else if (i < First)
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.Write((TotalLastMonth - First + i + 1).ToString().PadLeft(3));
                    Console.ResetColor();
                }
                //上个月

                else if (i >= (First + Total))
                {
                    Console.ForegroundColor = ConsoleColor.DarkGray;
                    Console.Write((i - Total - First + 1).ToString().PadLeft(3));
                    Console.ResetColor();
                }
                //下个月
                if (i % 7 == 0)
                    Console.WriteLine();
            }
        }
    }
}
